package com.qianbao.qianbaobusiness.ui.push.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidbase.utils.DisplayUtils;
import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.baseandroid.utils.FormatUtil;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.MessageListBean;
import com.qianbao.qianbaobusiness.ui.push.widget.SlidingButtonView;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeMessageAdapter extends BaseRecycleViewAdapter<MessageListBean.ResultItem> implements SlidingButtonView.IonSlidingButtonListener {

    Context mContext;
    OnItemClickListener mOnItemClickListener;
    SlidingButtonView mMenu = null;

    public HomeMessageAdapter(Context context, OnItemClickListener onItemClickListener) {
        super(context);
        mContext = context;
        mOnItemClickListener = onItemClickListener;
    }

    @Override
    protected void onBindBaseViewHolder(RecyclerView.ViewHolder holder, MessageListBean.ResultItem item) {
        //设置内容布局的宽为屏幕宽度
        ((MessageListHolder) holder).msg_content_rl.getLayoutParams().width = DisplayUtils
                .getWidth();
        ((MessageListHolder) holder).msg_unread_iv.setVisibility(View.VISIBLE);
        ((MessageListHolder) holder).msg_time_tv.setText(FormatUtil.changeTime(item.getSendendtime(), "yyyy/MM/dd HH:mm"));
        ((MessageListHolder) holder).msg_content_tv.setText(item.getTitle());
        if (item.getReadstatus().equals("0")) {
            ((MessageListHolder) holder).msg_unread_iv.setVisibility(View.VISIBLE);
            ((MessageListHolder) holder).msg_read_tv.setVisibility(View.VISIBLE);
            ((MessageListHolder) holder).msg_read_tv.setTag(item);
            ((MessageListHolder) holder).msg_read_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onReadMsgBtnClick(v, v.getTag());
                    item.setReadstatus("1");
                    notifyDataSetChanged();
                }
            });
        } else {
            ((MessageListHolder) holder).msg_unread_iv.setVisibility(View.GONE);
            ((MessageListHolder) holder).msg_read_tv.setVisibility(View.GONE);
        }
        ((MessageListHolder) holder).msg_delete_tv.setTag(item);
        ((MessageListHolder) holder).msg_delete_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onDeleteBtnCilck(v, v.getTag());
                getData().remove(item);
                notifyItemRemoved(holder.getLayoutPosition() - getHeaderLayoutCount());
            }
        });
        ((MessageListHolder) holder).msg_content_rl.setTag(item);
        ((MessageListHolder) holder).msg_content_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menuIsOpen()) {
                    closeMenu();
                } else {
                    mOnItemClickListener.onItemClick(v, v.getTag());
                    item.setReadstatus("1");
                    notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected RecyclerView.ViewHolder onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        return new MessageListHolder(View.inflate(mContext, R.layout.message_list_item, null));
    }

    @Override
    protected int getBaseItemViewType(int position) {
        return 0;
    }

    class MessageListHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.slide_button_id)
        SlidingButtonView slide_button_id;
        @BindView(R.id.msg_read_tv)
        TextView msg_read_tv;
        @BindView(R.id.msg_delete_tv)
        TextView msg_delete_tv;

        @BindView(R.id.msg_content_rl)
        RelativeLayout msg_content_rl;
        @BindView(R.id.msg_unread_iv)
        ImageView msg_unread_iv;
        @BindView(R.id.msg_time_tv)
        TextView msg_time_tv;
        @BindView(R.id.msg_content_tv)
        TextView msg_content_tv;

        MessageListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            setupView();
        }

        private void setupView() {
            slide_button_id.setSlidingButtonListener(HomeMessageAdapter.this);
        }
    }

    public interface OnItemClickListener {

        <T> void onItemClick(View view, T t);

        <T> void onDeleteBtnCilck(View view, T t);

        <T> void onReadMsgBtnClick(View view, T t);
    }

    @Override
    public void onMenuIsOpen(View view) {
        mMenu = (SlidingButtonView) view;
    }

    @Override
    public void onDownOrMove(SlidingButtonView slidingButtonView) {
        if (menuIsOpen()) {
            if (mMenu != slidingButtonView) {
                closeMenu();
            }
        }
    }

    public Boolean menuIsOpen() {
        if (mMenu != null) {
            return true;
        }
        return false;
    }

    public void closeMenu() {
        mMenu.closeMenu();
        mMenu = null;
    }

}
